import router from '@/router'
import {post} from '@/util/request'
import {getAddress} from '@/plugin/Map/Mapaddress'

export default {
	namespace:'scheList',
	namespaced:true,
	state:{
		isLoading:false,
		isModalLoading:false,
		isSubmiting:false,
		pageData:{},
		viewData:{}
	},
	mutations:{
		changeLoading(state,v){
			state.isLoading = v;
		},
		changeModalLoading(state,v){
			state.isModalLoading = v;
		},
		changeSubmit(state,v){
			state.isSubmiting = v;
		},
		changePagedata(state,v){
			state.pageData = v;
		},
		changeViewData(state,v){
			state.viewData = v;
		}
	},
	actions:{
		async reload({commit}, data) {
			commit('changeLoading',true);
			let res = await post('/atten/scheduling/list',data);
			// let addr = await getAddress(res.data.data.thisPageElements.map(v=>[v.lng,v.lat]));
			commit('changePagedata',res.data.data);
			commit('changeLoading',false);
		},
		deleteSche({commit,state},form){
			commit('changeModalLoading',true);
			return post('/atten/scheduling/deleteSchedul',form).then(data=>{
				commit('changeModalLoading',false);
				return data;
			})
		},
		async loadView({commit},id){
			commit('changeModalLoading',true);
			let res = await post('/atten/scheduling/view',{id});
			commit('changeViewData',res.data.data);
			commit('changeModalLoading',false);
		},
		getAddress({commit},[lng,lat]){
			return getAddress([[lng,lat]]).then(res => {
				return res;
			});
		},
		add({commit,state},form){
			commit('changeSubmit',true);
			return post('/atten/scheduling/addSchedul',form).then(data=>{
				commit('changeSubmit',false);
				return data;
			})
		},
		update({commit,state},form){
			commit('changeSubmit',true);
			return post('/atten/scheduling/updateSchedul',form).then(data=>{
				commit('changeSubmit',false);
				return data;
			})
		}
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				dispatch('reload',{})
			}
		})
	}
}
